'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 抽奖失败后花钻抽奖
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var battleRecordModel = require('../../../../models/battleRecordModel');
var luckyUtil = require('../../../../util/luckyUtil');
var rewardService = require('../../../../services/rewardService');
var roleService = require('../../../../services/roleService');
var PlayerManager = require('../../../../libs/playerManager');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var record_id = msg.record_id;
    if (!record_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var battle_record_model;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                battleRecordModel.getRecordByAtkRoleId(role_id, record_id, cb);
            },
            function (model, cb) {
                battle_record_model = model;

                if (!battle_record_model) {
                    return next(null, {
                        code: Code.BATTLE_RECORD_NOT_EXIST
                    });
                }

                // 判断是否需要重试
                if (!!battle_record_model.canLucky()) {
                    return next(null, {
                        code: Code.BATTLE_LUCKY_DONT_NEED_RETRY
                    });
                }
                cb();
            },
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;

                // 重新抽奖
                battle_record_model.retryLucky();

                // 获取需要扣除的钻石数量
                var retry_diamond = luckyUtil.getRetryDiamond(battle_record_model.getRetryNum());
                if (!retry_diamond) {
                    return next(null, {
                        code: Code.BATTLE_LUCKY_RETRY_DIAMOND
                    });
                }

                // 扣除钻石
                if (!roleService.subDiamond(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_RELUCKY, retry_diamond)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                cb();
            },
            function (cb) {
                battle_record_model.save();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
